var express = require('express');
var router = express.Router();
var mongodb= require('mongodb').MongoClient;
var str='mongodb://localhost:27017/aaa';

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

//注册
router.post('/register',(req,res)=>{
  var data=req.body
  mongodb.connect(str,(err,database)=>{
    database.collection('user',(err,coll)=>{
      coll.insertOne(data,()=>{
        res.send('1')
        database.close()
      })  
    })
  })
})

//登陆
router.post('/login',(req,res)=>{
  var datas=req.body
  console.log(datas)
  mongodb.connect(str,(err,database)=>{
    database.collection('user',(err,coll)=>{
      coll.find(datas).toArray((err,data)=>{
        if(data.length>0){
          req.session.user=data[0].user
          res.send('1')
        }else{
          res.send('2')
        }
        database.close()
      })
    })
  })
})
module.exports = router;
